package com.easy.order.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.Order;
import com.easy.common.service.IOrderService;
import com.easy.order.mapper.OrderMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Override
    public Order getOrderById(Integer id) {
        return super.getById(id);
    }

    @Override
    public int addOrder(Order order) {
        return super.save(order) ? 1 : 0;
    }

    @Override
    public int updateOrder(Order order) {
        return super.updateById(order) ? 1 : 0;
    }

    @Override
    public int deleteOrder(Integer id) {
        Order order = super.getById(id);
        if (order != null) {
            order.setIsdel(1);
            return super.updateById(order) ? 1 : 0;
        }
        return 0;
    }

    @Override
    public List<Order> getAll(Order order) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(order.getStart_address() != null, Order::getStart_address, order.getStart_address());
        queryWrapper.eq(order.getUser_id() != null, Order::getUser_id, order.getUser_id());
        queryWrapper.eq(order.getDriver_id() != null, Order::getDriver_id, order.getDriver_id());
        queryWrapper.eq(order.getStatus() != null, Order::getStatus, order.getStatus());
        queryWrapper.eq(Order::getIsdel, 0);
        return super.list(queryWrapper);
    }

    @Override
    public IPage<Order> getPage(Order order, IPage page) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(order.getUser_id() != null, Order::getUser_id, order.getUser_id());
        queryWrapper.eq(order.getDriver_id() != null, Order::getDriver_id, order.getDriver_id());
        queryWrapper.eq(order.getStatus() != null, Order::getStatus, order.getStatus());
        queryWrapper.eq(Order::getIsdel, 0);
        return super.page(page, queryWrapper);
    }
}